#include<bits/stdc++.h>

using namespace std;
bool a[10];
int table[]={0,0,0,0,0,0,10000,36,720,360,80,252,108,72,54,180,72,180,119,36,306,1080,144,1800,3600};
int b[5][5];
int main(){
    std::ios::sync_with_stdio(false);
    memset(a,false,9);
    int x,y,pos,x1,y1;
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++){
            cin>>b[i][j];
            a[b[i][j]]=true;
            if(b[i][j]==0)
            {
                x=i;
                y=j;
            }
        }        
    for(int i=0;i<9;i++)
        if(!a[i])
            b[x][y]=i;
    int k=3;
    while(k){
        cin>>x1>>y1;
        cout<<b[x1][y1]<<"\n";
        k--;
    }       
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++){
            b[i][0]+=b[i][j];
            b[0][i]+=b[j][i]; 
            }        
    b[0][0]=b[1][1]+b[2][2]+b[3][3];
    b[0][4]=b[3][1]+b[2][2]+b[1][3];
    cin>>pos;
    if(pos==7)
        cout<<table[b[0][0]]<<"\n";
    else if(pos==8)
        cout<<table[b[0][4]]<<"\n";
    else if(pos>=1&&pos<=3)
        cout<<table[b[pos][0]]<<"\n";
    else 
        cout<<table[b[0][pos-3]]<<"\n";                               
    return 0;
}